using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Sanchay
{
    public partial class AddCustomer : Form
    {
        bool isValid;
        TextBox m_CustomerTextBox;
      
        public AddCustomer(TextBox CustomerTextBox)
        {
          
            InitializeComponent();
            m_CustomerTextBox = CustomerTextBox;
            txtCustomerName.Text = CustomerTextBox.Text;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            // Mandatory Fields
            Control[] MandatoryControls = new Control[1];
            MandatoryControls[0] = txtCustomerName;
           
            if (Validation.ValidateEmpty(MandatoryControls))
            {
                SaveCustomer();
                isValid = true;
                this.Close();
            }
            else
                isValid = false;
        }

        private void SaveCustomer()
        {
            DataAccess da = new DataAccess();
            string query = "INSERT INTO Customer (Name, Address, ContactNo) VALUES (@Name, @Address, @ContactNo)";
            SqlParameter[] param = new SqlParameter[3];

            param[0] = new SqlParameter("Name", txtCustomerName.Text);
            param[1] = new SqlParameter("Address", txtAddress.Text);
            param[2] = new SqlParameter("ContactNo", txtContactNo.Text);

            da.DoCommand(query, param);
        }

        private void SaveCustomer(string CustomerName)
        {
            DataAccess da = new DataAccess();
            string query = "INSERT INTO Customer (Name, Address, ContactNo) VALUES (@Name, @Address, @ContactNo)";
            SqlParameter[] param = new SqlParameter[3];

            param[0] = new SqlParameter("Name", CustomerName);
            param[1] = new SqlParameter("Address", "");
            param[2] = new SqlParameter("ContactNo", "");

            da.DoCommand(query, param);
        }

   

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            isValid = true;
            this.Close();
        }

     
        private void AddCustomer_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.S)
                btnSave_Click(sender, e);
            if (e.KeyCode == Keys.Escape)
                BtnCancel_Click(sender, e);
        }

        private void AddCustomer_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!isValid)
                e.Cancel = true;
            m_CustomerTextBox.Text = txtCustomerName.Text;
        }

        private void AddCustomer_Load(object sender, EventArgs e)
        {
            
        }

    
    }
}